<template>
	<view class="index-container">
		<u-navbar :title="title" bgColor="#98c39a" :placeholder="true" leftIcon="">
		</u-navbar>
		<view class="banner">
			<u-swiper :list="bannerList" keyName="img" showTitle :autoplay="true" circular @click="bannerClick">
			</u-swiper>
		</view>
		<view class="application">
			<view class="tuijian">
				<u-text text="推荐应用" class="tuijiancontent"></u-text>
			</view>
			<view class="">
				<u-grid :border="false" col="4">
					<u-grid-item v-for="(listItem,listIndex) in applicationList" :key="listIndex"
						@click="applicationClick(listItem.applicationLabel)">
						<u-icon :customStyle="{paddingTop:20+'rpx'}"
							:name="'https://www.gzzyue.com/prod-api'+listItem.applicationLogo" :size="30"></u-icon>
						<text class="grid-text">{{listItem.applicationLabel}}</text>
					</u-grid-item>
				</u-grid>
			</view>
		</view>
		<view class="course">
			<view class=" todaycourse">
				<u-text text="今日课程"></u-text>
			</view>
			<view class="title">
				<u-list>
					<u-list-item v-for="(item, index) in courseList" :key="index">
						<u-cell size="large" :title="item.subjectName"
							:value="item.state==1?'课程准备':(item.state==2?'授课中':'课程结束')"
							:label="item.classname+'第'+item.whatClass+'节'+' '+item.teachingStartTime" isLink
							@click="toCourseDetail(item.classname,item.subjectName,item.whatClass,item.teachingStartTime)">
						</u-cell>
					</u-list-item>
				</u-list>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				title: '首页',
				bannerList: [],
				applicationList: [],
				courseList: [],
				urls: []
			}
		},
		onLoad() {
			this.getBanner()
			this.getApplicationList()
			this.loadmore()
			this.getCourseList()
		},
		methods: {
			toCourseDetail(classname, courseName, whatClass, teachingStartTime) {
				console.log("classname:ccccccccccc", classname, courseName, whatClass, teachingStartTime)
				uni.navigateTo({
					url: '/pages/index/courseDetail?classname=' + classname + '&courseName=' + courseName +
						'&whatClass=' + whatClass + '&teachingStartTime=' + teachingStartTime
				})
			},
			applicationClick(applicationLabel) {
				console.log(applicationLabel)
				switch (applicationLabel) {
					case '访客管理':
						uni.navigateTo({
							url: '/pages/vistor/vistor'
						});
						break;
					case '校园通知':
						uni.navigateTo({
							url: '/pages/notice/notice'
						});
						break;
					case '请假管理':
						uni.navigateTo({
							url: '/pages/askfor/askfor'
						});
						break;
					case '离校管理':
						uni.navigateTo({
							url: '/pages/leave/leave'
						});
						break;
					case '教师考勤':
						uni.navigateTo({
							url: '/pages/checking/checking'
						});
						break;
					case '一卡通':
						uni.navigateTo({

							url: '/pages/card/card'
						});
						break;
				}
			},
			loadmore() {
				for (let i = 0; i < 30; i++) {
					this.courseList.push({
						url: this.urls[uni.$u.random(0, this.urls.length - 1)]
					})
				}
			},
			bannerClick(index) {
				console.log(index, this.bannerList[index].id)

				uni.navigateTo({
					url: '/pages/index/bannerDetail?id=' + this.bannerList[index].id
				})
			},
			getBanner() {
				this.$request({
					url: '/prod-api/system/homePageRotation/list',
					method: 'GET',
					data: {}
				}).then(res => {
					if (res.statusCode == 200) {
						if (res.data.code == 200) {
							for (let s of res.data.rows) {
								this.bannerList.push({
									id: s.id,
									img: "https://www.gzzyue.com/prod-api" + s.rotationMap
								})
								// console.log("https://www.gzzyue.com/prod-api" + s.rotationMap)
							}
							console.log("bannerList:", this.bannerList)
						} else if (res.data.code == 500) {
							uni.$u.toast(res.data.msg)
						} else {
							uni.$u.toast('系统服务异常，请联系管理员！')
						}
					} else {
						uni.$u.toast('系统服务异常，请联系管理员！')
					}
				}).catch(err => {
					console.log('errors:', errors);
					uni.$u.toast('服务器错误，请联系管理员！')
				})
				// uni.request({
				// 	url: 'https://www.gzzyue.com/prod-api/system/homePageRotation/list',
				// 	method: 'GET',
				// 	header: {
				// 		'Authorization': uni.getStorageSync("token") //自定义请求头信息
				// 	},
				// 	success: (res) => {
				// 		console.log('banner:', res);
				// 		if (res.statusCode == 200) {
				// 			if (res.data.code == 200) {
				// 				// for(var i = 0;i<res.data.rows.length;i++){
				// 				// 	this.bannerList.push("https://www.gzzyue.com/prod-api"+res.data.rows[i].rotationMap)
				// 				// 	// console.log("https://www.gzzyue.com/prod-api"+res.data.rows[i].rotationMap)
				// 				// }
				// 				// for(let i in res.data.rows){
				// 				// 	this.bannerList.push("https://www.gzzyue.com/prod-api"+res.data.rows[i].rotationMap)
				// 				// 	console.log("https://www.gzzyue.com/prod-api"+res.data.rows[i].rotationMap)
				// 				// }
				// 				for (let s of res.data.rows) {
				// 					this.bannerList.push({
				// 						id:s.id,
				// 						img:"https://www.gzzyue.com/prod-api" + s.rotationMap
				// 					})
				// 					// console.log("https://www.gzzyue.com/prod-api" + s.rotationMap)
				// 				}
				// 				console.log("bannerList:",this.bannerList)
				// 			} else if (res.data.code == 500) {
				// 				uni.$u.toast(res.data.msg)
				// 			} else {
				// 				uni.$u.toast('系统服务异常，请联系管理员！')
				// 			}
				// 		} else {
				// 			uni.$u.toast('系统服务异常，请联系管理员！')
				// 		}
				// 	},
				// 	fail: (errors) => {
				// 		console.log('errors:', errors);
				// 		uni.$u.toast('服务器错误，请联系管理员！')
				// 	},
				// });
			},
			getApplicationList() {
				uni.request({
					url: 'https://www.gzzyue.com/prod-api/system/application/list',
					method: 'GET',
					header: {
						'Authorization': uni.getStorageSync("token") //自定义请求头信息
					},
					success: (res) => {
						console.log('applicationList:', res);
						if (res.statusCode == 200) {
							if (res.data.code == 200) {
								this.applicationList = res.data.rows

							} else if (res.data.code == 500) {
								uni.$u.toast(res.data.msg)
							} else {
								uni.$u.toast('系统服务异常，请联系管理员！')
							}
						} else {
							uni.$u.toast('系统服务异常，请联系管理员！')
						}
					},
					fail: (errors) => {
						console.log('errors:', errors);
						uni.$u.toast('服务器错误，请联系管理员！')
					},
				});
			},
			getCourseList() {
				uni.request({
					url: 'https://www.gzzyue.com/prod-api/system/SchoolCourseManage/list',
					method: 'GET',
					header: {
						'Authorization': uni.getStorageSync("token") //自定义请求头信息
					},
					success: (res) => {
						console.log('getCourseList:', res);
						if (res.statusCode == 200) {
							if (res.data.code == 200) {
								this.courseList = res.data.rows
							} else if (res.data.code == 500) {
								uni.$u.toast(res.data.msg)
							} else {
								uni.$u.toast('系统服务异常，请联系管理员！')
							}
						} else {
							uni.$u.toast('系统服务异常，请联系管理员！')
						}
					},
					fail: (errors) => {
						console.log('errors:', errors);
						uni.$u.toast('服务器错误，请联系管理员！')
					},
				});
			},
		},

	}
</script>
<style scoped lang="scss">
	.index-container {
		.banner {
			margin: 5px;

		}

		.application {
			.tuijian {
				padding: 0px 0 5px 5px;
				border-bottom: 1px solid #d1d1d1;

			}

			.grid-text {
				font-size: 12px;
			}
		}

		.course {
			.todaycourse {
				padding: 10px 0 5px 5px;
				border-bottom: 1px solid #d1d1d1;
			}

			.title {

				margin: 10px;
			}
		}
	}
</style>
